//
//  AIInfoSetView.m
//  MiaMind
//
//  Created by Vill on 2023/12/14.
//

#import "AIInfoSetView.h"
#import "AIGradientTabView.h"
#import "AIInfoFirstTableCell.h"

@interface AIInfoSetView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIButton *closeBtn;  //关闭
@property (nonatomic, strong) UIView *botoomView;
@property (nonatomic, strong) UIVisualEffectView *blurView;
@property (nonatomic, strong) AIGradientTabView *tableView;

@end

@implementation AIInfoSetView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        [self setUi];
    }
    return self;
}

-(void)setUi{
    [self addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(34));
        make.trailing.mas_equalTo(self).mas_offset(-PTW(13));
        make.top.mas_equalTo(self).mas_offset(KStatusBarHeight);
    }];
    [self addSubview:self.botoomView];
    [self.botoomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(PTW(400)+KTabBarHeight);
    }];
    [self.botoomView addSubview:self.blurView];
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.botoomView);
    }];
    
    [self.botoomView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.botoomView);
    }];
}

#pragma mark ------------------- lazy ------------------
- (UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:KImaged(@"ai_main_close") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIView *)botoomView{
    if(!_botoomView){
        _botoomView = [UIView new];
        _botoomView.backgroundColor = [UIColor colorHex:@"#FFFFFF" alpha:0.2];
    }
    return _botoomView;
}

- (UIVisualEffectView *)blurView{
    if(!_blurView){
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _blurView.alpha = 0.2;
    }
    return _blurView;
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    [self.botoomView clipsCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:PTW(20)];
}

- (AIGradientTabView *)tableView{
    if(!_tableView){
        _tableView = [[AIGradientTabView alloc] initWithFrame:CGRectMake(0,KScreenHeight - PTW(400) - KTabBarHeight, KScreenWidth, PTW(400)+KTabBarHeight) direction:AIGradualDirectionTop gradualValue:@.3f];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)closeAction:(UIButton*)btn{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (self.closeClickBlock){
            self.closeClickBlock();
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AIInfoFirstTableCell *cell = [AIInfoFirstTableCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PTW(73);
}

@end
